package com.ruoyi.user.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.domain.YmTypeEntity;
import com.ruoyi.user.enums.ReqHandleExceptionEnum;
import com.ruoyi.user.pojo.ReqHandleVo;
import com.ruoyi.user.service.IYmTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品类型 Controller
 * 
 * @author zq
 * @date 2022-11-08
 */
@RestController
@RequestMapping("/ymapp/type")
@Slf4j
public class YmTypeController extends BaseController
{
    @Autowired
    private IYmTypeService ymTypeService;

    /**
     * 查询所有分类信息
     */
//    @PreAuthorize("@ss.hasPermi('myapp:Type:getTypes')")
    @GetMapping("/getTypes")
    public ReqHandleVo<YmTypeEntity> getTypes()
    {
        ReqHandleVo<YmTypeEntity> reqHandleVo = new ReqHandleVo<>();
        List<YmTypeEntity> ymTypes = ymTypeService.selectYmTypeList(null);
        if(CollectionUtils.isEmpty(ymTypes)){
            reqHandleVo = reqHandleVo.buildError(ReqHandleExceptionEnum.TYPE_NOT_EXIST.getDesc());
//            log.info("reqHandleVo：{}",reqHandleVo);
            return reqHandleVo;
        }
        reqHandleVo.setData(ymTypes);
        reqHandleVo = reqHandleVo.buildSuccess("请求成功");
//        log.info("reqHandleVo：{}",reqHandleVo);
        return reqHandleVo;
    }

    /**
     * 查询列表
     */
//    @PreAuthorize("@ss.hasPermi('myapp:Type:list')")
    @GetMapping("/list")
    public TableDataInfo list(YmTypeEntity ymTypeEntity)
    {
        startPage();
        List<YmTypeEntity> list = ymTypeService.selectYmTypeList(ymTypeEntity);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @PreAuthorize("@ss.hasPermi('myapp:Type:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YmTypeEntity ymTypeEntity)
    {
        List<YmTypeEntity> list = ymTypeService.selectYmTypeList(ymTypeEntity);
        ExcelUtil<YmTypeEntity> util = new ExcelUtil<YmTypeEntity>(YmTypeEntity.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('myapp:Type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ymTypeService.selectYmTypeById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('myapp:Type:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YmTypeEntity ymTypeEntity)
    {
        return toAjax(ymTypeService.insertYmType(ymTypeEntity));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('myapp:Type:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YmTypeEntity ymTypeEntity)
    {
        return toAjax(ymTypeService.updateYmType(ymTypeEntity));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('myapp:Type:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ymTypeService.deleteYmTypeByIds(ids));
    }
}
